<template>
  <basic-container>
    <avue-crud
      :option="option"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      :table-loading="loading"
      v-model="form"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
      @selection-change="selectionChange"
      :key="timer"
    >
      <template slot="menuRight">
        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>
      </template>
      <template slot="menu" slot-scope="{ row }">
        <el-button @click="detailEvent(row)" type="text" size="small" icon="el-icon-view">待拆包详情</el-button>
      </template>
    </avue-crud>
    <el-dialog append-to-body :visible.sync="flagDialog" title="待拆包详情">
      <avue-crud ref="crud2" :option="optionDialog" :data="dataDialog" @current-change="currentDetailChange" @size-change="sizeDetailChange" :table-loading="detailLoading">
      </avue-crud>
    </el-dialog>
    <!-- <export-excel ref="exportExcel" url="/api/mes-warehouse/material/export" name="备料单执行记录" :option="option" :page="page" :query="query"></export-excel> -->
  </basic-container>
</template>

<script>
import { getList, getDetail } from '@/api/warehouse/stockorderDetail';
import { mapGetters } from 'vuex';
import exportExcel from '@/components/export-excel/exp.vue';
import userTableConfig from '@/components/user-table-config/index.vue';

export default {
  components: {
    exportExcel,
    userTableConfig
  },
  data() {
    return {
      pageCode: 'stockorderDetail',
      form: {},
      query: {},
      detailLoading: false,
      loading: true,
      timer: new Date().getTime(),
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      detailPage: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      params: {},
      selectionList: [],
      flagDialog: false,
      currentRow: {},
      options: [],
      option: {
        height: 'calc(100vh - 520px)',
        columnBtn: false,
        calcHeight: 30,
        tip: false,
        // searchMenuSpan: 10,
        refreshBtn: true, //刷新按钮是否显示
        searchBtn: true, // 显示查询的按钮
        searchSpan: 5,
        border: true,
        index: true,
        dialogClickModal: false,
        addBtn: false,
        delBtn: false,
        editBtn: false,
        viewBtn: false,
        width: '100',
        searchLabelWidth: 130,
        searchMenuPosition: 'right',
        searchShow: true,
        menuWidth: 130,
        indexLabel: '序号',
        column: [
          {
            label: '原包装条码',
            prop: 'packSn',
            searchOrder: 1,
            search: true
          },
          {
            label: '原包装数量',
            prop: 'packQuantity',
            searchOrder: 5
          },
          {
            label: '物料编码',
            prop: 'partNo',
            search: true
          },
          {
            label: '物料名称',
            prop: 'partName',
            searchOrder: 2
          },
          {
            label: '待拆包个数',
            prop: 'packCount',
            searchOrder: 2
          },
          {
            label: '仓库',
            prop: 'inventoryName',
            searchOrder: 2,
            search: true
          }
        ]
      },
      optionDialog: {
        height: 'calc(100vh - 400px)',
        calcHeight: 30,
        delBtn: false,
        editBtn: false,
        addBtn: false,
        refreshBtn: false, //刷新按钮是否显示
        columnBtn: false, //控制表格列是否显示按钮是否显示
        border: true,
        menuWidth: 100,
        menu: false, //表格操作栏是否显示
        indexLabel: '序号',
        column: [
          {
            label: '原包装条码',
            prop: 'packSn'
          },
          {
            label: '备料单号',
            prop: 'sourceNo'
          },
          {
            label: '物料编码',
            prop: 'partNo'
          },
          {
            label: '物料名称',
            prop: 'partName'
          },
          {
            label: '需求数量（原包装）',
            prop: 'newPackQuantity'
          },
          {
            label: '原包装数量',
            prop: 'packQuantity'
          },
          {
            label: '新包装条码',
            prop: 'newPackSn'
          },
          {
            label: '新包装数量',
            prop: 'newPackQuantity'
          },
          {
            label: '状态',
            prop: 'packStatusName',
            searchLabelWidth: 90,
            span: 4
          }
        ]
      },
      data: [],
      dataDialog: []
    };
  },
  filters: {
    statusConvert(val) {
      switch (val) {
        case 1:
          return '正常';
        case 2:
          return '全部锁定';
        case 3:
          return '部分锁定';
        case 4:
          return '报废';
        case 5:
          return '已出库';
        case 6:
          return '已入库';
        default:
          break;
      }
    }
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        // viewBtn: this.vaildData(this.permission.material_view, false)
        // addBtn: this.vaildData(this.permission.material_add, false),
        // delBtn: this.vaildData(this.permission.material_delete, false),
        // editBtn: this.vaildData(this.permission.material_edit, false)
      };
    },
    isHasPermission() {
      // return this.permission.material_detail;
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(',');
    }
  },
  methods: {
    init() {},
    detailEvent(row) {
      this.currentRow = row;
      this.flagDialog = true;
      this.getPackDetail(row, this.detailPage);
    },
    getPackDetail(row, page) {
      this.detailLoading = true;
      getDetail({
        packNo: row.packSn
      }).then((res) => {
        let data = res.data.data;
        this.dataDialog = data || [];
        this.detailLoading = false;
        this.detailPage.total = data.total;
      });
    },
    beforeOpen(done, type) {
      done();
    },
    searchReset(params, done) {
      this.query = {};
      this.page.currentPage = 1;
      this.page.pageSize = 15;
      this.onLoad(this.page, this.query);
    },
    searchChange(params, done) {
      if (params.recommendWarehouse) {
        params.warehouseName = params.recommendWarehouse;
        delete params.recommendWarehouse;
      }
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, this.query);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.onLoad(this.page, this.params);
    },
    currentDetailChange(currentPage) {
      this.detailPage.currentPage = currentPage;
      console.log(currentPage, 3333333333);
      this.getPackDetail(this.currentRow, this.detailPage);
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.onLoad(this.page);
    },
    sizDetailChange(pageSize) {
      this.detailPage.pageSize = pageSize;
      console.log(pageSize, 113333333333);
      this.getPackDetail(this.currentRow, this.detailPage);
    },
    refreshChange() {
      this.onLoad(this.page);
    },
    onLoad(page, params = {}) {
      console.log(page, 'page');
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.data = data.records;
        this.page.total = data.total;
        this.loading = false;
      });
    },
    newKey() {
      this.timer = new Date().getTime();
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  watch: {}
};
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__header {
  padding: 16px 24px;
  min-height: 20px;
  border-bottom: 1px solid #f0f0f0;
}
::v-deep .el-dialog__body {
  padding: 15px 24px;
  .avue-crud__menu {
    display: none;
  }
}
</style>
